﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.Sql;
using System.Data.SqlClient;

using System.Configuration;  

namespace LOGIN
{
    public partial class Orders : Form
    {
        
        public Orders()
        {
            InitializeComponent();
            this.comb_Supplyname.DropDownStyle = ComboBoxStyle.DropDownList;
          
        }
        

        private void OrdersOK_Click(object sender, EventArgs e)
        {
            if (this.Order.Text.Trim() == "")
            {
                MessageBox.Show("订单不能为空！");
                this.Order.Focus();
                return;
            }
            if (this.comb_Supplyname.ValueMember == "")
            {
                MessageBox.Show("供应商不能为空！");
                this.Order.Focus();
                return;
            }
            SqlConnection sqlConnection = new SqlConnection();                                              
            sqlConnection.ConnectionString =
            ConfigurationManager.ConnectionStrings["Sql"].ConnectionString;                        
            SqlCommand sqlCommand = new SqlCommand();                                                       
            sqlCommand.Connection = sqlConnection;
            sqlCommand.CommandText =
                "INSERT orders (SupplyID,OrderKinds,OrderDate,GetDate,ProducePla) VALUES(@SupplyID,@OrderKinds,@OrderDate,@GetDate,@ProducePla);";                 
            sqlCommand.Parameters.AddWithValue("@SupplyID",comb_Supplyname.SelectedValue);
            sqlCommand.Parameters.AddWithValue("@OrderKinds", this.Order.Text.Trim());
            sqlCommand.Parameters.AddWithValue("@OrderDate", this.dateTimePicker1.Value);
            sqlCommand.Parameters.AddWithValue("@GetDate", this.dateTimePicker2.Value);
            sqlCommand.Parameters.AddWithValue("@ProducePla", this.Address.Text.Trim());
            sqlConnection.Open();                                                                    
            int rowAffected = sqlCommand.ExecuteNonQuery();                                  
            sqlConnection.Close();                                                                     
            if (rowAffected == 1)                                                                     
            {
                MessageBox.Show("添加成功。");
                ordersHelper.order = Order.Text.Trim();
                if (dateTimePicker2.Value == DateTime.Now)
                {
                    ordersHelper.state = "入库";
                }
                else {
                    ordersHelper.state = "未入库";
                }
            }
            else                                                                                       
            {
                MessageBox.Show("添加失败！");                                                          
            }
         
        }

       

        

        private void OrdersClear_Click(object sender, EventArgs e)
        {
            Order.Text = null;
            Address.Text = null;
        }


        private void Orders_Load(object sender, EventArgs e)
        {
            SqlConnection sqlConnection = new SqlConnection();                                           
            sqlConnection.ConnectionString =
            ConfigurationManager.ConnectionStrings["Sql"].ConnectionString;                             
            SqlCommand sqlCommand = new SqlCommand();                                                      
            sqlCommand.Connection = sqlConnection;                                                                                                  
            sqlCommand.CommandText = "SELECT * FROM supplyer;";                                            
            SqlDataAdapter sqlDataAdapter = new SqlDataAdapter();                                          
            sqlDataAdapter.SelectCommand = sqlCommand;                                                      
            DataTable supplyer = new DataTable();                                                         
            sqlConnection.Open();                                                                        
            sqlDataAdapter.Fill(supplyer);                                                               
            this.comb_Supplyname.DataSource = supplyer;                                                         
            this.comb_Supplyname.DisplayMember = "Supplyname";                                                          
            this.comb_Supplyname.ValueMember = "SupplyID";                                                           
            SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();
            if (sqlDataReader.Read())                                                                       
            {

                this.comb_Supplyname.SelectedValue = (int)sqlDataReader["SupplyID"];

            }
            sqlDataReader.Close();
            sqlConnection.Close();  
        }

        private void Toorderdetails_Click(object sender, EventArgs e)
        {
            Orderdetails Orderdetails = new Orderdetails();
            Orderdetails.Show();
            
        }
        }
       
        
    }

